﻿using Modbus;
using Newtonsoft.Json;
using PrintLabel.Model;
using PrintLabel.Tools;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 纺织贸易管理系统;

namespace PrintLabel.View
{
    public partial class SettingFrm : AntdUI.Window
    {
        private Modbus.ComService comService = new Modbus.ComService();
        private Parameter param = new Parameter() { BaudRate = "9600", Database = "Textiletradedb", Server = "192.168.0.109,1433", Stopwatch = "德莱宝", ComNum = "Com1" };
        public SettingFrm(Parameter parameter)
        {
            InitializeComponent();
            this.param = parameter;
        }

        private void SettingFrm_Load(object sender, EventArgs e)
        {
            try
            {
                cmbCom.Items.AddRange(ComService.获取所有端口().ToArray());
                cmbCom.Text = param.ComNum;
            }
            catch (Exception ex)
            {
                ShowMessage.ShowErrorMessagebox(this, "获取COM口的时候发生了错误：" + ex.Message);
            }
            try
            {
                cmbmabiao.Items.AddRange(StopwatchSyle.StopwatchList.ToArray());
                cmbmabiao.Text = param.Stopwatch;
            }
            catch (Exception ex)
            {
                ShowMessage.ShowErrorMessagebox(this, "获取码表类型的时候发生了错误：" + ex.Message);
            }
            cmbBoteli.Text = param.BaudRate;
            sltwaijian.Text = param.OutsideInspection;
            sltPlace.Text = param.OutsideInspectionPlace;
            sltPlace.Items.AddRange(dbContext.dbcontext.Queryable<LXR>().Where(x => x.LX == "供货商").Select(x => x.MC).ToArray());
            sltPlace.Enabled = false;
            checkbox1.Checked = param.AutoWeight;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            param.BaudRate = cmbBoteli.Text;
            param.ComNum = cmbCom.Text;
            param.Stopwatch = cmbmabiao.Text;
            param.OutsideInspection = sltwaijian.Text;
            param.OutsideInspectionPlace = sltPlace.Text;
            param.AutoWeight=checkbox1.Checked;
            File.WriteAllText(Application.StartupPath + "//Setting.json", JsonConvert.SerializeObject(param));
            this.Close();
        }

        private void select1_SelectedIndexChanged(object sender, AntdUI.IntEventArgs e)
        {
            if (!sltPlace.Enabled)
            {
                var fm = new 密码输入();
                fm.ShowDialog();
                var yh = dbContext.dbcontext.Queryable<Yhb>().First(x => x.YHBH == "10001");
                if (yh != null)
                {
                    if (yh.MM == fm.密码)
                    {
                        sltPlace.Enabled = true;
                    }
                    else
                    {
                        ShowMessage.ShowErrorMessagebox(this, "密码错误，请重新输入！");
                        sltPlace.Enabled = false;
                    }
                }
                else
                {
                    ShowMessage.ShowErrorMessagebox(this, "没有找到，请联系管理员！");
                }
            }
        }
    }
}
